import Route from '@ioc:Adonis/Core/Route'
import UsersController from "App/Controllers/Http/UsersController";
import SpusController from "App/Controllers/Http/SpusController";
import Application from '@ioc:Adonis/Core/Application'
import Hash from "@ioc:Adonis/Core/Hash";
import Redis from "@ioc:Adonis/Addons/Redis";
import LogsController from "App/Controllers/Http/LogsController";
import uuid from 'uuid-v4'
/**
 * 添加用户
 */
Route.post('/users/add', async (ctx) => {
    return new UsersController().addUser(ctx);
})
/**
 * 获取用户信息
 */
Route.get('/users/listUser', async (ctx) => {
    return new UsersController().listUser(ctx);
})
/**
 * 删除用户
 */
Route.post('/users/deleteUser', async (ctx) => {
    return new UsersController().deleteUser(ctx);
})

/**
 * 商城客户列表
 */
Route.post('/customer/list', async (ctx) => {
    return new UsersController().addUser(ctx);
})
/*
 * 添加Spu,Sku,以及规格参数信息
 */
Route.post('/spus/addSpu', async (ctx) => {
    return new SpusController().addSpu(ctx);
});
/**
 * 获取Spu信息
 */
Route.post('/spus/spuList', async (ctx) => {
    return new SpusController().getSpuList(ctx);
});
/**
 * 图片上传接口
 */
Route.post('/upload/images', async ({ request }) => {
    const images = request.files('file');
    for (let image of images) {
       await image.move(Application.tmpPath('uploads'));
    }
    let host=request.headers()['x-forwarded-for'];
    return {url:"http://"+host+"/uploads/"+images[0].clientName};
});
/**
 * 获取Spu信息
 */
Route.post("/spus/getSpuByCondition",async (ctx)=>{
    let token=uuid();
    let spusController=new SpusController();
    let data=await spusController.getSpuListByCondition(ctx);
    ctx.session.put('sumbitOrderToken', token);
    console.log("写入session参数",token);
    return {data:data,sumbitOrderToken:token};
});
//根据名称搜索
Route.post("/spus/getSpuListByName",async (ctx)=>{
      return new SpusController().getSpuListByName(ctx);
});
//删除商品,级联
Route.post("/spus/deleteProduct",async (ctx)=>{
      return new SpusController().deleteProduct(ctx);
});
//用户登录
Route.post('/login', async (ctx) => {
      const usersController=new UsersController();
      let users:any=await usersController.getUser(ctx);
      let user=users[0];
      const params:any=ctx.request.body();
      let isVerify=await Hash.verify(user&&user.password,params.password);
      if(isVerify){
           ctx.session.put('loginUser', user);
           return {status:0}
      }
      return {status:1}
});
/**
 * 获取登录用户信息
 */
Route.get('/users/loginInfo', async (ctx) => {
    const loginUser = ctx.session&&ctx.session.get('loginUser');
    return loginUser;
});
/**
 * 用户注册
 */
Route.post('/users/register', async (ctx) => {
    const usersController=new UsersController();
    await usersController.addUser(ctx);
});
/**
 * 退出登录
 */
Route.get('/users/unLogin', async (ctx) => {
     const usersController=new UsersController();
     await usersController.unLogin(ctx);
});
/**
 * 下单
 */
Route.post('/users/addOrder', async (ctx) => {
      let sumbitOrderToken=await ctx.session.get('sumbitOrderToken');
      const params:any=ctx.request.body().sumbitOrderToken;
      console.log("前端的参数:",params);
      console.log("session中的参数:",sumbitOrderToken);
      if(sumbitOrderToken!==params){
          return {status:3};
      }
      ctx.session.forget("sumbitOrderToken");
      const loginUser = ctx.session.get('loginUser');
      if(loginUser){
         const userController=new UsersController();
         let res=await userController.addOrder(ctx);
         return res;
      }
      return {status:1};

});
/**
 * 查询用户订单
 */
Route.post('/users/listUserOrder', async (ctx) => {
  const loginUser = ctx.session.get('loginUser');
  if(loginUser){
      const userController=new UsersController();
      let res=await userController.listUserOrder(ctx);
      return res;
  }
  return {status:1};

});
/**
 * 删除用户订单
 */
Route.post('/users/deleteUserOrder', async (ctx) => {
    const loginUser = ctx.session.get('loginUser');
    if(loginUser){
        let userController=new UsersController();
        await userController.deleteUserOrder(ctx);
    }
    return {status:1};
});
/**
 * 查询用户日志
 */
Route.post('/users/listUserLogs', async (ctx) => {
    let userController=new UsersController();
    return await userController.listUserLogs(ctx);
});
/**
 * 删除用户日志
 */
Route.post('/users/deleteUserLogs', async (ctx) => {
    const loginUser = ctx.session.get('loginUser');
    if(loginUser){
      let userController=new UsersController();
      await userController.listUserLogs(ctx);
    }
    return {status:1};
});
Redis.subscribe('log:userActions', (logInfo: string) => {
      new LogsController().addLog(JSON.parse(logInfo));
});
Route.get('/test1', async (ctx) => {
        console.log(ctx);
        let inorderTraversal=function(root:any){
            const res:any=[];
            let predecessor:any=null;
            while(root){
                if(root.left){
                    predecessor=root.left;
                    while(predecessor.right&&predecessor.right!==root){
                        predecessor=predecessor.right;
                    }
                    if(!predecessor.right){
                        predecessor.right=root;
                        root=root.left;
                    }else{
                        res.push(root.text);
                        predecessor.right=null;
                        root=root.right;
                    }

                }else{
                    res.push(root.text);
                    root=root.right;
                }
            }
            return res;
        }
        let root1:any=new Node("1");
        let root2:any=new Node("2");
        let root3:any=new Node("3");
        let root4:any=new Node("4");
        let root5:any=new Node("5");
        let root6:any=new Node("6");
        root1.left=root2;
        root2.left=root4;
        root2.right=root5;
        root1.right=root3;
        root3.left=root6;
        let res=inorderTraversal(root1);
        console.log(res);
  }
);
class Node{
    constructor(text:any){
        this.text=text;
    } 
    left:Node;
    right:Node;
    text:any="";
}